package com.zhiyou100.action;

import com.zhiyou100.entity.Student;
import com.zhiyou100.util.json.FastJsonTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * 响应 json
 *
 * @author yang
 * @className Test01
 * @packageName com.zhiyou100.action
 * @Description TODO
 * @date 2020-08-24 09:41
 */
@Controller
@RequestMapping("/test01")
public class Test01 {
    @RequestMapping("json01.action")
    public void json01(HttpServletResponse response) {
        final Student student = new Student("杨磊", 19, "男");

        final String s = FastJsonTools.objectToJson(student);
        try {
            response.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/json02.action")
    //@RequestMapping(value = "/json02.action",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String json02() throws UnsupportedEncodingException {
        // 创建一个对象
        final Student student = new Student("杨磊", 19, "女");

        // 返回对象的 json 格式
        //由于@ResponseBody注解，这里会将str转成json格式返回；十分方便
        final String s = FastJsonTools.objectToJson(student);
        System.out.println(s);
        return s;
    }

    @RequestMapping("/json03.action")
    //@RequestMapping(value = "/json02.action",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String json03() throws UnsupportedEncodingException {
        // 返回一个字符串，普通字符串

        return "abcdefg";
    }

    @RequestMapping("/json04.action")
    //@RequestMapping(value = "/json02.action",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String json04() throws UnsupportedEncodingException {
        // 返回指定的字符串
        // 创建一个对象
        final Student student = new Student("杨磊", 19, "女");

        return student.toString();
    }
}
